1 DATA

## Longitudinal data
data_sum <- loadlongitudinaldata(dataset = "DATA_Adults_G1G29.csv", rm_generation1 = 1,rm_generation2 = 7,rm_generation3 = 29)

## Phenotyping steps
data_G0 <- loadfitnessdata(dataset = "Selection_Phenotypage_G0_G7_G8.csv", generation = "G1")
data_G7 <- loadfitnessdata(dataset = "Selection_Phenotypage_G0_G7_G8.csv", generation = "G7")
data_G29 <- loadfitnessdata(dataset = 
                            "PERFORMANCE_Comptage_adultes_G13G14G15G16G17G18G19G20G21G22G23G24G25G26G27G28G29.csv",
                            generation = "29")
head(data_sum)
##   Line Fruit_s Generation         Phase  N Nb_adults       sd    fitness
## 1  CE1  Cherry          2 first_prepool 20   9.15000 6.123939 -0.7819784
## 2  CE1  Cherry          3 first_prepool 10  15.30000 7.631077 -0.2678794
## 3  CE1  Cherry          4 first_prepool  8  15.00000 7.782765 -0.2876821
## 4  CE1  Cherry          5 first_prepool  6  14.50000 6.284903 -0.3215836
## 5  CE2  Cherry          2 first_prepool 20   7.75000 7.758696 -0.9480394
## 6  CE2  Cherry          3 first_prepool  7  14.42857 8.303757 -0.3265219
##   se_fitness
## 1  0.1496562
## 2  0.1577228
## 3  0.1834415
## 4  0.1769518
## 5  0.2238577
## 6  0.2175215
head(data_G0)
##     Treatment Line Fruit_s Nb_eggs Nb_adults SA Emergence_rate
## 993    Cherry  CE1      GF      76         6  0     0.07894737
## 994    Cherry  CE1      GF      89        17  0     0.19101124
## 995    Cherry  CE1      GF      57        12  0     0.21052632
## 996    Cherry  CE1      GF     172        24  0     0.13953488
## 997    Cherry  CE1      GF     173        33  0     0.19075145
## 998    Cherry  CE1      GF      91        18  0     0.19780220
head(data_G7)
##    Treatment Line    Fruit_s Nb_eggs Nb_adults SA Emergence_rate
## 3 Strawberry  CR4  Cranberry     152        68  0      0.4473684
## 4  Cranberry  CR4  Cranberry     246        25  1      0.1016260
## 5     Cherry  CR4  Cranberry     238        29  0      0.1218487
## 6     Cherry  CR4  Cranberry     166        23  0      0.1385542
## 8  Cranberry  FR3 Strawberry     204         5  0      0.0245098
## 9 Strawberry  FR3 Strawberry     124        45  1      0.3629032
head(data_G29)
##       Treatment Line Fruit_s Nb_eggs Nb_adults SA Emergence_rate
## 5392 Strawberry  CEA  Cherry     196        16  0     0.08163265
## 5393 Strawberry  CEA  Cherry     192        30  0     0.15625000
## 5394 Strawberry  CEA  Cherry     160        17  0     0.10625000
## 5395 Strawberry  CEA  Cherry     106         9  0     0.08490566
## 5396 Strawberry  CEA  Cherry     119        14  0     0.11764706
## 5397 Strawberry  CEA  Cherry     204        24  0     0.11764706
dim(data_G29)
## [1] 990   7
#Compute log change
data_logchange <- computelogchange(fitness_dataset_intermediate = data_G7, fitness_dataset_final = data_G29)
#Lines: CE2 and CR2 do not pass Geary's test (the threshold of a standardized mean greater than 3)
data_logchange$Line_type  <- ifelse(data_logchange$Line == "CR2"| data_logchange$Line == "CE2", "solid","dashed")


#Formatting for testing correlation
TEMP_dataG7_CheCran <- formattinglogchange(data_logchange, "7", "Cherry", "Cranberry")
TEMP_dataG7_CranStraw <- formattinglogchange(data_logchange, "7", "Cranberry", "Strawberry")
TEMP_dataG7_StrawChe <- formattinglogchange(data_logchange, "7", "Strawberry", "Cherry")

TEMP_dataG29_CheCran <- formattinglogchange(data_logchange, "29", "Cherry", "Cranberry")
TEMP_dataG29_CranStraw <- formattinglogchange(data_logchange, "29", "Cranberry", "Strawberry")
TEMP_dataG29_StrawChe <- formattinglogchange(data_logchange, "29", "Strawberry", "Cherry")

2 ALL FRUITS

## Longitudinal data
data_sum_allfruits <- loadlongitudinaldata_allfruits(dataset = "DATA_Adults_G1G29.csv", rm_generation1 = 1,rm_generation2 = 7,rm_generation3 = 29)

data_sum_allfruits
##     Line      Fruit_s Generation         Phase  N  Nb_adults         sd
## 1    CA1 Blackcurrant          2 first_prepool 20  2.3500000  3.2971279
## 2    CA1 Blackcurrant          3 first_prepool  2  0.5000000  0.7071068
## 3    CA2 Blackcurrant          2 first_prepool 11  1.0000000  1.5491933
## 4    CA3 Blackcurrant          2 first_prepool 12  2.0833333  2.6097138
## 5    CA3 Blackcurrant          3 first_prepool  1  1.0000000         NA
## 6    CA4 Blackcurrant          2 first_prepool 12  1.6666667  1.4974726
## 7    CA4 Blackcurrant          3 first_prepool  1  0.0000000         NA
## 8    CA5 Blackcurrant          2 first_prepool 12  2.1666667  2.5166115
## 9    CA5 Blackcurrant          3 first_prepool  1  5.0000000         NA
## 10   CE1       Cherry          2 first_prepool 20  9.1500000  6.1239392
## 11   CE1       Cherry          3 first_prepool 10 15.3000000  7.6310768
## 12   CE1       Cherry          4 first_prepool  8 15.0000000  7.7827648
## 13   CE1       Cherry          5 first_prepool  6 14.5000000  6.2849025
## 14   CE2       Cherry          2 first_prepool 20  7.7500000  7.7586963
## 15   CE2       Cherry          3 first_prepool  7 14.4285714  8.3037570
## 16   CE2       Cherry          4 first_prepool  5 15.4000000  7.8930349
## 17   CE2       Cherry          5 first_prepool  4  8.2500000  4.3493295
## 18   CE3       Cherry          2 first_prepool 20  8.7000000  8.2020537
## 19   CE3       Cherry          3 first_prepool  8 20.3750000  9.7532046
## 20   CE3       Cherry          4 first_prepool  9 19.1111111  7.0079320
## 21   CE3       Cherry          5 first_prepool  8 15.3750000 10.9013433
## 22   CE4       Cherry          2 first_prepool 20 12.5000000  6.8402832
## 23   CE4       Cherry          3 first_prepool 12 16.3333333  9.4034165
## 24   CE4       Cherry          4 first_prepool 10 12.5000000  6.8190908
## 25   CE4       Cherry          5 first_prepool  6  9.6666667  3.9327683
## 26   CE5       Cherry          2 first_prepool 20  5.9000000  5.6837719
## 27   CE5       Cherry          3 first_prepool  5  5.4000000  6.0249481
## 28   CE5       Cherry          4 first_prepool  1 16.0000000         NA
## 29   CR1    Cranberry          2 first_prepool 20 19.7000000  7.4275448
## 30   CR1    Cranberry          3 first_prepool 20  9.5000000  6.6054204
## 31   CR1    Cranberry          4 first_prepool 10  8.5000000  4.1432676
## 32   CR1    Cranberry          5 first_prepool  4  5.0000000  8.0415587
## 33   CR2    Cranberry          2 first_prepool 20 13.6500000  8.4559229
## 34   CR2    Cranberry          3 first_prepool 15 13.2666667  8.9479979
## 35   CR2    Cranberry          4 first_prepool 10  9.6000000  7.5159091
## 36   CR2    Cranberry          5 first_prepool  5 10.8000000  5.8906706
## 37   CR3    Cranberry          2 first_prepool 20 25.1000000  6.2314989
## 38   CR3    Cranberry          3 first_prepool 20 17.0500000 10.5355289
## 39   CR3    Cranberry          4 first_prepool 17 13.1176471  7.7126710
## 40   CR3    Cranberry          5 first_prepool 11 12.8181818 10.0081785
## 41   CR4    Cranberry          2 first_prepool 20 19.6500000  9.2524534
## 42   CR4    Cranberry          3 first_prepool 20 16.8000000 11.3211493
## 43   CR4    Cranberry          4 first_prepool 16  6.5000000  8.5401015
## 44   CR4    Cranberry          5 first_prepool  5  5.6000000  7.5365775
## 45   CR5    Cranberry          2 first_prepool 18 18.8333333  8.7464278
## 46   CR5    Cranberry          3 first_prepool 16  6.4375000  5.7383941
## 47   CR5    Cranberry          4 first_prepool  5 11.0000000  5.1478151
## 48   CR5    Cranberry          5 first_prepool  3  9.0000000  5.5677644
## 49   EG1     Rosehips          2 first_prepool  7  0.5714286  0.5345225
## 50   EG2     Rosehips          2 first_prepool 12  0.6666667  0.8876254
## 51   EG3     Rosehips          2 first_prepool  7  2.2857143  2.6903708
## 52   EG4     Rosehips          2 first_prepool 11  5.0000000  5.9160798
## 53   EG4     Rosehips          3 first_prepool  2  9.5000000 10.6066017
## 54   EG4     Rosehips          4 first_prepool  1 19.0000000         NA
## 55   EG5     Rosehips          2 first_prepool 18  3.3333333  4.2702803
## 56   EG5     Rosehips          3 first_prepool  2  9.5000000  0.7071068
## 57   EG5     Rosehips          4 first_prepool  1  1.0000000         NA
## 58   FI1          Fig          2 first_prepool 20  9.2000000  9.6223424
## 59   FI1          Fig          3 first_prepool  9 18.1111111  4.6755867
## 60   FI1          Fig          4 first_prepool  8  7.8750000  6.2435681
## 61   FI2          Fig          2 first_prepool 20 15.1500000  7.4992982
## 62   FI2          Fig          3 first_prepool 15  7.1333333  6.8333914
## 63   FI2          Fig          4 first_prepool  6  3.5000000  4.3243497
## 64   FI3          Fig          2 first_prepool 20 11.8500000  5.7241409
## 65   FI3          Fig          3 first_prepool 11  8.7272727  5.3682569
## 66   FI3          Fig          4 first_prepool  5  2.4000000  4.2778499
## 67   FI4          Fig          2 first_prepool 11  5.8181818  5.7239529
## 68   FI4          Fig          3 first_prepool  3  7.0000000 12.1243557
## 69   FI4          Fig          4 first_prepool  1  1.0000000         NA
## 70   FI5          Fig          2 first_prepool 13  3.8461538  6.5300096
## 71   FI5          Fig          3 first_prepool  2 11.0000000  4.2426407
## 72   FI5          Fig          4 first_prepool  1  2.0000000         NA
## 73   FR1   Strawberry          2 first_prepool 20 16.8500000  7.0582166
## 74   FR1   Strawberry          3 first_prepool 18 16.1111111  8.5052848
## 75   FR1   Strawberry          4 first_prepool 15 16.2000000  7.6082662
## 76   FR1   Strawberry          5 first_prepool 11 25.8181818 10.9162098
## 77   FR2   Strawberry          2 first_prepool 20  7.7500000  7.0178344
## 78   FR2   Strawberry          3 first_prepool  7 11.8571429  9.4238805
## 79   FR2   Strawberry          4 first_prepool  5 16.6000000 10.1390335
## 80   FR2   Strawberry          5 first_prepool  5  6.4000000  7.4027022
## 81   FR3   Strawberry          2 first_prepool 20 14.0500000 11.4131734
## 82   FR3   Strawberry          3 first_prepool 13 14.6923077  6.0880294
## 83   FR3   Strawberry          4 first_prepool 10 11.8000000 10.7061768
## 84   FR3   Strawberry          5 first_prepool  5  9.4000000  7.7974355
## 85   FR4   Strawberry          2 first_prepool 20 14.1000000  7.0404844
## 86   FR4   Strawberry          3 first_prepool 15 12.0000000  8.1940745
## 87   FR4   Strawberry          4 first_prepool  9 16.7777778 13.7638819
## 88   FR4   Strawberry          5 first_prepool  8 13.8750000 10.8947891
## 89   FR5   Strawberry          2 first_prepool 20  8.2000000  8.2945765
## 90   FR5   Strawberry          3 first_prepool  7  7.1428571  7.8619033
## 91   FR5   Strawberry          4 first_prepool  2  9.0000000  9.8994949
## 92   FR5   Strawberry          5 first_prepool  1  9.0000000         NA
## 93   RA1        Grape          2 first_prepool  8  3.3750000  2.5035689
## 94   RA1        Grape          3 first_prepool  1  5.0000000         NA
## 95   RA2        Grape          2 first_prepool  5  0.6000000  1.3416408
## 96   RA3        Grape          2 first_prepool  5  2.8000000  3.1144823
## 97   RA4        Grape          2 first_prepool  5  0.6000000  1.3416408
## 98   RA5        Grape          2 first_prepool  9  2.0000000  2.5000000
## 99   TO1       Tomato          2 first_prepool  7  5.1428571  9.4415495
## 100  TO1       Tomato          3 first_prepool  3  6.0000000  6.2449980
## 101  TO1       Tomato          4 first_prepool  1 12.0000000         NA
## 102  TO2       Tomato          2 first_prepool  2  8.0000000  9.8994949
## 103  TO2       Tomato          3 first_prepool  1  7.0000000         NA
## 104  TO3       Tomato          2 first_prepool  9  4.1111111  7.5571894
## 105  TO3       Tomato          3 first_prepool  1 21.0000000         NA
## 106  TO3       Tomato          4 first_prepool  1  5.0000000         NA
## 107  TO4       Tomato          2 first_prepool  2  0.5000000  0.7071068
## 108  TO5       Tomato          2 first_prepool  3  1.6666667  2.8867513
##         fitness se_fitness
## 1   -2.14131695 0.31372775
## 2   -3.68887945 1.00000000
## 3   -2.99573227 0.46709937
## 4   -2.26176310 0.36161255
## 5   -2.99573227         NA
## 6   -2.48490665 0.25936987
## 7          -Inf         NA
## 8   -2.22254239 0.33529992
## 9   -1.38629436         NA
## 10  -0.78197839 0.14965622
## 11  -0.26787945 0.15772277
## 12  -0.28768207 0.18344153
## 13  -0.32158362 0.17695177
## 14  -0.94803943 0.22385771
## 15  -0.32652191 0.21752155
## 16  -0.26136476 0.22921250
## 17  -0.88551907 0.26359572
## 18  -0.83240925 0.21080862
## 19   0.01857639 0.16924067
## 20  -0.04546237 0.12223137
## 21  -0.26298946 0.25068012
## 22  -0.47000363 0.12236271
## 23  -0.20252426 0.16619587
## 24  -0.47000363 0.17251087
## 25  -0.72704873 0.16609096
## 26  -1.22077992 0.21541187
## 27  -1.30933332 0.49897013
## 28  -0.22314355         NA
## 29  -0.01511364 0.08430708
## 30  -0.74444047 0.15547546
## 31  -0.85566611 0.15414309
## 32  -1.38629436 0.80415587
## 33  -0.38199275 0.13852028
## 34  -0.41047765 0.17414797
## 35  -0.73396918 0.24757699
## 36  -0.61618614 0.24392481
## 37   0.22713557 0.05551416
## 38  -0.15958207 0.13817102
## 39  -0.42177385 0.14260160
## 40  -0.44486766 0.23541399
## 41  -0.01765494 0.10528812
## 42  -0.17435339 0.15068369
## 43  -1.12393010 0.32846544
## 44  -1.27296568 0.60186784
## 45  -0.06010392 0.10946298
## 46  -1.13359201 0.22285026
## 47  -0.59783700 0.20928844
## 48  -0.79850770 0.35717225
## 49  -3.55534806 0.35355339
## 50  -3.40119738 0.38435306
## 51  -2.16905370 0.44487826
## 52  -1.38629436 0.35675303
## 53  -0.74444047 0.78947368
## 54  -0.05129329         NA
## 55  -1.79175947 0.30195442
## 56  -0.74444047 0.05263158
## 57  -2.99573227         NA
## 58  -0.77652879 0.23387187
## 59  -0.09920665 0.08605374
## 60  -0.93203909 0.28030917
## 61  -0.27773174 0.11068608
## 62  -1.03095364 0.24734216
## 63  -1.74296931 0.50440239
## 64  -0.52340441 0.10801323
## 65  -0.82927935 0.18546348
## 66  -2.12026354 0.79713027
## 67  -1.23474446 0.29662819
## 68  -1.04982212 1.00000000
## 69  -2.99573227         NA
## 70  -1.64865863 0.47088569
## 71  -0.59783700 0.27272727
## 72  -2.30258509         NA
## 73  -0.17138162 0.09366559
## 74  -0.21622311 0.12443058
## 75  -0.21072103 0.12126209
## 76   0.25534669 0.12748230
## 77  -0.94803943 0.20248200
## 78  -0.52280181 0.30040053
## 79  -0.18632958 0.27315142
## 80  -1.13943428 0.51727954
## 81  -0.35310988 0.18164151
## 82  -0.30840820 0.11492514
## 83  -0.52763274 0.28691444
## 84  -0.75502258 0.37097012
## 85  -0.34955748 0.11165249
## 86  -0.51082562 0.17630841
## 87  -0.17567701 0.27345461
## 88  -0.36564361 0.27761367
## 89  -0.89159812 0.22618582
## 90  -1.02961942 0.41601282
## 91  -0.79850770 0.77777778
## 92  -0.79850770         NA
## 93  -1.77933695 0.26226526
## 94  -1.38629436         NA
## 95  -3.50655790 1.00000000
## 96  -1.96611286 0.49744244
## 97  -3.50655790 1.00000000
## 98  -2.30258509 0.41666667
## 99  -1.35812348 0.69388867
## 100 -1.20397280 0.60092521
## 101 -0.51082562         NA
## 102 -0.91629073 0.87500000
## 103 -1.04982212         NA
## 104 -1.58203894 0.61274508
## 105  0.04879016         NA
## 106 -1.38629436         NA
## 107 -3.68887945 1.00000000
## 108 -2.48490665 1.00000000
pd <- position_dodge(0.3) # move them .05 to the left and right


PLOT_ALLFRUITS<-ggplot(data=data_sum_allfruits, 
                            aes(x=factor(Generation),group=Line, y=fitness, colour=Fruit_s)) + 
  facet_wrap(~ Fruit_s, nrow = 8) +
  geom_errorbar(aes(ymin=fitness-1.96*se_fitness, ymax=fitness+1.96*se_fitness),
                width=.1,position=pd,size=0.2,color="black") +
  geom_line(size=0.3,position=pd) +
  geom_point(size=1, position=pd,shape=21, fill="white") +
  ylab("Fitness") + 
  xlab("Generation") + 
  theme_LO_sober
PLOT_ALLFRUITS

cowplot::save_plot(file =here::here("figures", "PLOT_AllFruits.pdf"), PLOT_ALLFRUITS, base_height = 17/cm(1), base_width = 15/cm(1), dpi = 1200)




###########Check CA4
data_sum_allfruits<-data_sum_allfruits[data_sum_allfruits$Line!="CA4",]


#Models
mod_all_interaction <- lm(fitness ~ Fruit_s*Generation, 
            weights = N, data = data_sum_allfruits)

mod_all_fruit_generation<- lm(fitness ~ Fruit_s+Generation , 
            weights = N, data = data_sum_allfruits)

mod_all_fruit<- lm(fitness ~ Fruit_s , 
            weights = N, data = data_sum_allfruits)

mod_all_null<- lm(fitness ~ 1 , 
            weights = N, data = data_sum_allfruits)

MuMIn::AICc(mod_all_interaction,mod_all_fruit_generation,mod_all_fruit,mod_all_null)
##                          df     AICc
## mod_all_interaction      17 202.1323
## mod_all_fruit_generation 10 207.8578
## mod_all_fruit             9 205.7438
## mod_all_null              2 301.0742
#Posthoc
emmeans::emmeans(mod_all_interaction, list(pairwise ~ Fruit_s), adjust = "tukey") #
## $`emmeans of Fruit_s`
##  Fruit_s      emmean     SE df lower.CL upper.CL
##  Blackcurrant -3.012 0.8241 90   -5.313  -0.7104
##  Cherry       -0.562 0.1062 90   -0.859  -0.2660
##  Cranberry    -0.444 0.0887 90   -0.692  -0.1965
##  Fig          -1.069 0.1532 90   -1.497  -0.6412
##  Grape        -1.250 1.6478 90   -5.851   3.3509
##  Rosehips     -1.429 0.4782 90   -2.764  -0.0936
##  Strawberry   -0.439 0.0972 90   -0.710  -0.1677
##  Tomato       -1.111 0.4618 90   -2.401   0.1783
## 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 8 estimates 
## 
## $`pairwise differences of Fruit_s`
##  contrast                  estimate    SE df t.ratio p.value
##  Blackcurrant - Cherry     -2.44904 0.831 90 -2.947  0.0752 
##  Blackcurrant - Cranberry  -2.56745 0.829 90 -3.098  0.0508 
##  Blackcurrant - Fig        -1.94262 0.838 90 -2.318  0.2958 
##  Blackcurrant - Grape      -1.76127 1.842 90 -0.956  0.9793 
##  Blackcurrant - Rosehips   -1.58273 0.953 90 -1.661  0.7118 
##  Blackcurrant - Strawberry -2.57259 0.830 90 -3.100  0.0504 
##  Blackcurrant - Tomato     -1.90029 0.945 90 -2.012  0.4801 
##  Cherry - Cranberry        -0.11841 0.138 90 -0.856  0.9891 
##  Cherry - Fig               0.50641 0.186 90  2.717  0.1309 
##  Cherry - Grape             0.68777 1.651 90  0.417  0.9999 
##  Cherry - Rosehips          0.86631 0.490 90  1.769  0.6425 
##  Cherry - Strawberry       -0.12355 0.144 90 -0.858  0.9889 
##  Cherry - Tomato            0.54875 0.474 90  1.158  0.9415 
##  Cranberry - Fig            0.62483 0.177 90  3.530  0.0146 
##  Cranberry - Grape          0.80618 1.650 90  0.489  0.9997 
##  Cranberry - Rosehips       0.98472 0.486 90  2.025  0.4713 
##  Cranberry - Strawberry    -0.00513 0.132 90 -0.039  1.0000 
##  Cranberry - Tomato         0.66716 0.470 90  1.419  0.8463 
##  Fig - Grape                0.18135 1.655 90  0.110  1.0000 
##  Fig - Rosehips             0.35989 0.502 90  0.717  0.9963 
##  Fig - Strawberry          -0.62996 0.181 90 -3.473  0.0173 
##  Fig - Tomato               0.04233 0.487 90  0.087  1.0000 
##  Grape - Rosehips           0.17854 1.716 90  0.104  1.0000 
##  Grape - Strawberry        -0.81132 1.651 90 -0.492  0.9997 
##  Grape - Tomato            -0.13902 1.711 90 -0.081  1.0000 
##  Rosehips - Strawberry     -0.98986 0.488 90 -2.029  0.4688 
##  Rosehips - Tomato         -0.31756 0.665 90 -0.478  0.9997 
##  Strawberry - Tomato        0.67230 0.472 90  1.425  0.8435 
## 
## P value adjustment: tukey method for comparing a family of 8 estimates
summary(mod_all_interaction)
## 
## Call:
## lm(formula = fitness ~ Fruit_s * Generation, data = data_sum_allfruits, 
##     weights = N)
## 
## Weighted Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.7941 -0.9849  0.1232  0.9455  3.0501 
## 
## Coefficients:
##                              Estimate Std. Error t value Pr(>|t|)
## (Intercept)                   -1.1887     1.5830  -0.751    0.455
## Fruit_sCherry                  0.0652     1.6115   0.040    0.968
## Fruit_sCranberry               1.6444     1.6091   1.022    0.310
## Fruit_sFig                     1.0779     1.6442   0.656    0.514
## Fruit_sGrape                  -3.5250     3.4243  -1.029    0.306
## Fruit_sRosehips               -2.6798     1.8532  -1.446    0.152
## Fruit_sStrawberry              0.4937     1.6093   0.307    0.760
## Fruit_sTomato                 -1.6412     1.9193  -0.855    0.395
## Generation                    -0.5837     0.7600  -0.768    0.444
## Fruit_sCherry:Generation       0.7634     0.7662   0.996    0.322
## Fruit_sCranberry:Generation    0.2956     0.7652   0.386    0.700
## Fruit_sFig:Generation          0.2769     0.7780   0.356    0.723
## Fruit_sGrape:Generation        1.6929     1.6728   1.012    0.314
## Fruit_sRosehips:Generation     1.3650     0.8799   1.551    0.124
## Fruit_sStrawberry:Generation   0.6657     0.7654   0.870    0.387
## Fruit_sTomato:Generation       1.1341     0.8869   1.279    0.204
## 
## Residual standard error: 1.468 on 90 degrees of freedom
## Multiple R-squared:  0.7222, Adjusted R-squared:  0.6759 
## F-statistic:  15.6 on 15 and 90 DF,  p-value: < 2.2e-16

3 ADAPTATION

3.1 Proportion of adaptation: raw data

tapply(data_sum$Nb_adults[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)
##     Cherry  Cranberry Strawberry 
##    8.80000   19.38667   12.19000
tapply(data_sum$Nb_adults[data_sum$Generation=="27"],data_sum$Fruit_s[data_sum$Generation=="27"],mean)
##     Cherry  Cranberry Strawberry 
##   35.84000   33.63750   42.33333
## Calcul proportion change between G2 and G7 
((tapply(data_sum$Nb_adults[data_sum$Generation=="27"],data_sum$Fruit_s[data_sum$Generation=="27"],mean) -
    tapply(data_sum$Nb_adults[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)) / 
  tapply(data_sum$Nb_adults[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)) * 100
##     Cherry  Cranberry Strawberry 
##  307.27273   73.50843  247.27919
# 
# (abs(tapply(data_sum$fitness[data_sum$Generation=="27"],data_sum$Fruit_s[data_sum$Generation=="27"],mean) -
#     tapply(data_sum$fitness[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)) / 
#   abs(tapply(data_sum$fitness[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean))) * 100

3.2 Analysis

######## Models
mod_nul <- lme4::lmer(fitness ~ 1 + (1|Generation), 
            weights = N, data = data_sum)

mod_Fruits <- lme4::lmer(fitness ~ Fruit_s + (1|Generation), 
            weights = N, data = data_sum)

mod_Generation <- lme4::lmer(fitness ~ Generation + (1|Generation), 
            weights = N, data = data_sum)

mod_Phase <- lme4::lmer(fitness~ Phase + (1|Generation), 
            weights = N, data = data_sum)

mod_Lines <- lme4::lmer(fitness~ Line + (1|Generation), 
            weights = N, data = data_sum)

mod_Generation_Phase <- lme4::lmer(fitness~ Phase + Generation + (1|Generation),  
            weights = N, data = data_sum)

mod_Generation_Fruits <- lme4::lmer(fitness ~ Generation + Fruit_s + (1|Generation), 
            weights = N, data = data_sum)

mod_Fruits_Phase <- lme4::lmer(fitness ~ Phase + Fruit_s + (1|Generation), 
            weights = N, data = data_sum)


mod_Generation_Fruits_Phase <- lme4::lmer(fitness ~ Generation + Phase + Fruit_s + (1|Generation), 
                              weights = N, data = data_sum)


mod_Doubleinteraction <- lme4::lmer(fitness ~ Generation + Phase + Fruit_s + 
                              Fruit_s:Generation + 
                              (1|Generation),  
                              weights = N, data = data_sum)

mod_Double_gene <- lme4::lmer(fitness ~ Generation*Fruit_s + 
                              (1|Generation), 
                              weights = N, data = data_sum)

mod_Double <- lme4::lmer(fitness ~ Phase*Fruit_s + 
                              (1|Generation), 
                              weights = N, data = data_sum)

mod_Double_Generation <- lme4::lmer(fitness ~ Generation + Phase*Fruit_s + 
                              (1|Generation), 
                              weights = N, data = data_sum)

mod_Tripleinteraction <- lme4::lmer(fitness ~ Generation*Phase*Fruit_s + 
                              (1|Generation), 
                              weights = N, data = data_sum)

mod_DoublePopulationGeneration <- lme4::lmer(fitness ~ Generation*Line + 
                              (1|Generation), 
                              weights = N, data = data_sum)


#  Add indic 
data_sum$indic_cherry<- ifelse(data_sum$Fruit_s=="Cherry", "Cherry", "Others")
data_sum$indic_cranberry <- ifelse(data_sum$Fruit_s=="Cranberry", "Cranberry", "Others")
data_sum$indic_strawberry <- ifelse(data_sum$Fruit_s=="Strawberry",  "Strawberry", "Others")

# Test whether the temporal dynamics are different in one fruit compared to the two other fruits 
mod_Cherry_Phase <- lme4::lmer(fitness ~ Phase*indic_cherry + (1|Generation),
            weights = N, data = data_sum)

mod_Cranberry_Phase <- lme4::lmer(fitness ~ Phase*indic_cranberry + (1|Generation),
            weights = N, data = data_sum)

mod_Strawberry_Phase <- lme4::lmer(fitness ~ Phase*indic_strawberry + (1|Generation),
            weights = N, data = data_sum)



# Model selection: AICC and Loglik
MuMIn::AICc(mod_nul,mod_Fruits,mod_Generation,mod_Phase,mod_Lines,mod_Generation_Phase,mod_Generation_Fruits,mod_Fruits_Phase,mod_Generation_Fruits_Phase,mod_Double_gene,mod_Doubleinteraction,mod_Double,mod_Double_Generation,mod_Tripleinteraction,mod_DoublePopulationGeneration,mod_Cherry_Phase,mod_Cranberry_Phase,mod_Strawberry_Phase)
##                                df     AICc
## mod_nul                         3 339.7756
## mod_Fruits                      5 333.0398
## mod_Generation                  4 334.4473
## mod_Phase                       5 314.8770
## mod_Lines                      31 366.1793
## mod_Generation_Phase            6 322.8487
## mod_Generation_Fruits           6 328.4534
## mod_Fruits_Phase                7 309.1298
## mod_Generation_Fruits_Phase     8 317.0215
## mod_Double_gene                 8 342.2655
## mod_Doubleinteraction          10 330.9505
## mod_Double                     11 317.2153
## mod_Double_Generation          12 325.1311
## mod_Tripleinteraction          20 354.5837
## mod_DoublePopulationGeneration 60 504.0109
## mod_Cherry_Phase                8 307.8003
## mod_Cranberry_Phase             8 328.4648
## mod_Strawberry_Phase            8 316.4444
MuMIn::model.sel(mod_nul,mod_Fruits,mod_Generation,mod_Phase,mod_Lines,mod_Generation_Phase,mod_Generation_Fruits,mod_Fruits_Phase,mod_Generation_Fruits_Phase,mod_Double_gene,mod_Doubleinteraction,mod_Double,mod_Double_Generation,mod_Tripleinteraction,mod_DoublePopulationGeneration,mod_Cherry_Phase,mod_Cranberry_Phase,mod_Strawberry_Phase)
## Model selection table 
##                                  (Int) Frt_s        Gnr Phs Lin Frt_s:Gnr
## mod_Cherry_Phase               -0.6076                    +              
## mod_Fruits_Phase               -0.3364     +              +              
## mod_Phase                      -0.4877                    +              
## mod_Strawberry_Phase           -0.5050                    +              
## mod_Generation_Fruits_Phase    -0.2913     + -0.0134300   +              
## mod_Double                     -0.6078     +              +              
## mod_Generation_Phase           -0.4455       -0.0127800   +              
## mod_Double_Generation          -0.5626     + -0.0137100   +              
## mod_Generation_Fruits          -0.1599     +  0.0388900                  
## mod_Cranberry_Phase            -0.4319                    +              
## mod_Doubleinteraction          -0.4975     + -0.0007595   +             +
## mod_Fruits                      0.4343     +                             
## mod_Generation                 -0.3267        0.0398800                  
## mod_nul                         0.2798                                   
## mod_Double_gene                -0.3736     +  0.0517500                 +
## mod_Tripleinteraction          -1.1070     +  0.1752000   +             +
## mod_Lines                      -0.5347                        +          
## mod_DoublePopulationGeneration -1.0460        0.1785000       +          
##                                Frt_s:Phs Gnr:Phs Frt_s:Gnr:Phs Gnr:Lin ind_chr
## mod_Cherry_Phase                                                             +
## mod_Fruits_Phase                                                              
## mod_Phase                                                                     
## mod_Strawberry_Phase                                                          
## mod_Generation_Fruits_Phase                                                   
## mod_Double                             +                                      
## mod_Generation_Phase                                                          
## mod_Double_Generation                  +                                      
## mod_Generation_Fruits                                                         
## mod_Cranberry_Phase                                                           
## mod_Doubleinteraction                                                         
## mod_Fruits                                                                    
## mod_Generation                                                                
## mod_nul                                                                       
## mod_Double_gene                                                               
## mod_Tripleinteraction                  +       +             +                
## mod_Lines                                                                     
## mod_DoublePopulationGeneration                                       +        
##                                ind_chr:Phs ind_crn ind_crn:Phs ind_str
## mod_Cherry_Phase                         +                            
## mod_Fruits_Phase                                                      
## mod_Phase                                                             
## mod_Strawberry_Phase                                                 +
## mod_Generation_Fruits_Phase                                           
## mod_Double                                                            
## mod_Generation_Phase                                                  
## mod_Double_Generation                                                 
## mod_Generation_Fruits                                                 
## mod_Cranberry_Phase                              +           +        
## mod_Doubleinteraction                                                 
## mod_Fruits                                                            
## mod_Generation                                                        
## mod_nul                                                               
## mod_Double_gene                                                       
## mod_Tripleinteraction                                                 
## mod_Lines                                                             
## mod_DoublePopulationGeneration                                        
##                                ind_str:Phs             family df   logLik  AICc
## mod_Cherry_Phase                           gaussian(identity)  8 -145.598 307.8
## mod_Fruits_Phase                           gaussian(identity)  7 -147.331 309.1
## mod_Phase                                  gaussian(identity)  5 -152.314 314.9
## mod_Strawberry_Phase                     + gaussian(identity)  8 -149.920 316.4
## mod_Generation_Fruits_Phase                gaussian(identity)  8 -150.208 317.0
## mod_Double                                 gaussian(identity) 11 -147.046 317.2
## mod_Generation_Phase                       gaussian(identity)  6 -155.249 322.8
## mod_Double_Generation                      gaussian(identity) 12 -149.899 325.1
## mod_Generation_Fruits                      gaussian(identity)  6 -158.052 328.5
## mod_Cranberry_Phase                        gaussian(identity)  8 -155.930 328.5
## mod_Doubleinteraction                      gaussian(identity) 10 -155.009 331.0
## mod_Fruits                                 gaussian(identity)  5 -161.395 333.0
## mod_Generation                             gaussian(identity)  4 -163.141 334.4
## mod_nul                                    gaussian(identity)  3 -166.838 339.8
## mod_Double_gene                            gaussian(identity)  8 -162.830 342.3
## mod_Tripleinteraction                      gaussian(identity) 20 -155.433 354.6
## mod_Lines                                  gaussian(identity) 31 -147.476 366.2
## mod_DoublePopulationGeneration             gaussian(identity) 60 -172.328 504.0
##                                 delta weight
## mod_Cherry_Phase                 0.00  0.634
## mod_Fruits_Phase                 1.33  0.326
## mod_Phase                        7.08  0.018
## mod_Strawberry_Phase             8.64  0.008
## mod_Generation_Fruits_Phase      9.22  0.006
## mod_Double                       9.41  0.006
## mod_Generation_Phase            15.05  0.000
## mod_Double_Generation           17.33  0.000
## mod_Generation_Fruits           20.65  0.000
## mod_Cranberry_Phase             20.66  0.000
## mod_Doubleinteraction           23.15  0.000
## mod_Fruits                      25.24  0.000
## mod_Generation                  26.65  0.000
## mod_nul                         31.98  0.000
## mod_Double_gene                 34.47  0.000
## mod_Tripleinteraction           46.78  0.000
## mod_Lines                       58.38  0.000
## mod_DoublePopulationGeneration 196.21  0.000
## Models ranked by AICc(x) 
## Random terms (all models): 
## '1 | Generation'
anova(mod_nul,mod_Fruits,mod_Generation,mod_Phase,mod_Lines,mod_Generation_Phase,mod_Generation_Fruits,mod_Fruits_Phase,mod_Generation_Fruits_Phase,mod_Doubleinteraction,mod_Double,mod_Double_Generation,mod_Tripleinteraction,mod_DoublePopulationGeneration,mod_Double_gene,mod_Cherry_Phase,mod_Cranberry_Phase,mod_Strawberry_Phase)
## Data: data_sum
## Models:
## mod_nul: fitness ~ 1 + (1 | Generation)
## mod_Generation: fitness ~ Generation + (1 | Generation)
## mod_Fruits: fitness ~ Fruit_s + (1 | Generation)
## mod_Phase: fitness ~ Phase + (1 | Generation)
## mod_Generation_Phase: fitness ~ Phase + Generation + (1 | Generation)
## mod_Generation_Fruits: fitness ~ Generation + Fruit_s + (1 | Generation)
## mod_Fruits_Phase: fitness ~ Phase + Fruit_s + (1 | Generation)
## mod_Generation_Fruits_Phase: fitness ~ Generation + Phase + Fruit_s + (1 | Generation)
## mod_Double_gene: fitness ~ Generation * Fruit_s + (1 | Generation)
## mod_Cherry_Phase: fitness ~ Phase * indic_cherry + (1 | Generation)
## mod_Cranberry_Phase: fitness ~ Phase * indic_cranberry + (1 | Generation)
## mod_Strawberry_Phase: fitness ~ Phase * indic_strawberry + (1 | Generation)
## mod_Doubleinteraction: fitness ~ Generation + Phase + Fruit_s + Fruit_s:Generation + 
## mod_Doubleinteraction:     (1 | Generation)
## mod_Double: fitness ~ Phase * Fruit_s + (1 | Generation)
## mod_Double_Generation: fitness ~ Generation + Phase * Fruit_s + (1 | Generation)
## mod_Tripleinteraction: fitness ~ Generation * Phase * Fruit_s + (1 | Generation)
## mod_Lines: fitness ~ Line + (1 | Generation)
## mod_DoublePopulationGeneration: fitness ~ Generation * Line + (1 | Generation)
##                                npar    AIC    BIC  logLik deviance   Chisq Df
## mod_nul                           3 336.65 347.18 -165.33   330.65           
## mod_Generation                    4 323.01 337.05 -157.50   315.01 15.6458  1
## mod_Fruits                        5 321.74 339.29 -155.87   311.74  3.2688  1
## mod_Phase                         5 305.39 322.93 -147.69   295.39 16.3510  0
## mod_Generation_Phase              6 305.95 327.01 -146.98   293.95  1.4362  1
## mod_Generation_Fruits             6 308.84 329.90 -148.42   296.84  0.0000  0
## mod_Fruits_Phase                  7 291.48 316.05 -138.74   277.48 19.3583  1
## mod_Generation_Fruits_Phase       8 291.95 320.02 -137.97   275.95  1.5325  1
## mod_Double_gene                   8 306.40 334.48 -145.20   290.40  0.0000  0
## mod_Cherry_Phase                  8 290.29 318.37 -137.15   274.29 16.1075  0
## mod_Cranberry_Phase               8 310.11 338.19 -147.06   294.11  0.0000  0
## mod_Strawberry_Phase              8 298.37 326.45 -141.19   282.37 11.7394  0
## mod_Doubleinteraction            10 289.56 324.65 -134.78   269.56 12.8159  2
## mod_Double                       11 291.19 329.79 -134.59   269.19  0.3679  1
## mod_Double_Generation            12 291.62 333.73 -133.81   267.62  1.5683  1
## mod_Tripleinteraction            20 290.03 360.22 -125.02   250.03 17.5887  8
## mod_Lines                        31 304.85 413.64 -121.42   242.85  7.1828 11
## mod_DoublePopulationGeneration   60 322.24 532.80 -101.12   202.24 40.6128 29
##                                Pr(>Chisq)    
## mod_nul                                      
## mod_Generation                  7.638e-05 ***
## mod_Fruits                       0.070610 .  
## mod_Phase                       < 2.2e-16 ***
## mod_Generation_Phase             0.230756    
## mod_Generation_Fruits            1.000000    
## mod_Fruits_Phase                1.083e-05 ***
## mod_Generation_Fruits_Phase      0.215736    
## mod_Double_gene                  1.000000    
## mod_Cherry_Phase                < 2.2e-16 ***
## mod_Cranberry_Phase              1.000000    
## mod_Strawberry_Phase            < 2.2e-16 ***
## mod_Doubleinteraction            0.001648 ** 
## mod_Double                       0.544137    
## mod_Double_Generation            0.210447    
## mod_Tripleinteraction            0.024530 *  
## mod_Lines                        0.784096    
## mod_DoublePopulationGeneration   0.074405 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Posthoc
mod_Best <- lme4::lmer(fitness ~ Phase*indic_cherry + (1|Generation),
            weights = N, data = data_sum)

emmeans::emmeans(mod_Best, list(pairwise ~ Phase:indic_cherry), adjust = "tukey") #
## $`emmeans of Phase, indic_cherry`
##  Phase           indic_cherry    emmean     SE     df lower.CL upper.CL
##  first_prepool   Cherry       -0.607592 0.1631    674   -1.038   -0.177
##  pool            Cherry        0.321562 0.1674 111144   -0.119    0.762
##  second_postpool Cherry        0.701026 0.0712   2598    0.514    0.889
##  first_prepool   Others       -0.442650 0.1288    340   -0.783   -0.102
##  pool            Others        0.000256 0.1106 107399   -0.291    0.291
##  second_postpool Others        0.455445 0.0585   1121    0.301    0.610
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 6 estimates 
## 
## $`pairwise differences of Phase, indic_cherry`
##  contrast                                        estimate     SE     df t.ratio
##  first_prepool Cherry - pool Cherry                -0.929 0.2337   2618 -3.976 
##  first_prepool Cherry - second_postpool Cherry     -1.309 0.1779    796 -7.354 
##  first_prepool Cherry - first_prepool Others       -0.165 0.1484  27353 -1.111 
##  first_prepool Cherry - pool Others                -0.608 0.1970   1380 -3.085 
##  first_prepool Cherry - second_postpool Others     -1.063 0.1733    710 -6.135 
##  pool Cherry - second_postpool Cherry              -0.379 0.1819  38241 -2.086 
##  pool Cherry - first_prepool Others                 0.764 0.2112   2298  3.619 
##  pool Cherry - pool Others                          0.321 0.1479 504858  2.173 
##  pool Cherry - second_postpool Others              -0.134 0.1773  34407 -0.755 
##  second_postpool Cherry - first_prepool Others      1.144 0.1471    475  7.773 
##  second_postpool Cherry - pool Others               0.701 0.1315  17346  5.328 
##  second_postpool Cherry - second_postpool Others    0.246 0.0614 125672  3.997 
##  first_prepool Others - pool Others                -0.443 0.1697    999 -2.609 
##  first_prepool Others - second_postpool Others     -0.898 0.1414    401 -6.350 
##  pool Others - second_postpool Others              -0.455 0.1251  13913 -3.638 
##  p.value
##  0.0010 
##  <.0001 
##  0.8770 
##  0.0253 
##  <.0001 
##  0.2946 
##  0.0041 
##  0.2505 
##  0.9748 
##  <.0001 
##  <.0001 
##  0.0009 
##  0.0959 
##  <.0001 
##  0.0037 
## 
## Degrees-of-freedom method: kenward-roger 
## P value adjustment: tukey method for comparing a family of 6 estimates
summary(mod_Best)
## Linear mixed model fit by REML ['lmerMod']
## Formula: fitness ~ Phase * indic_cherry + (1 | Generation)
##    Data: data_sum
## Weights: N
## 
## REML criterion at convergence: 291.2
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.7742 -0.4669  0.1121  0.5198  2.5009 
## 
## Random effects:
##  Groups     Name        Variance Std.Dev.
##  Generation (Intercept) 0.03638  0.1907  
##  Residual               3.11306  1.7644  
## Number of obs: 247, groups:  Generation, 24
## 
## Fixed effects:
##                                         Estimate Std. Error t value
## (Intercept)                              -0.6076     0.1604  -3.788
## Phasepool                                 0.9292     0.2318   4.008
## Phasesecond_postpool                      1.3086     0.1753   7.465
## indic_cherryOthers                        0.1649     0.1483   1.112
## Phasepool:indic_cherryOthers             -0.4862     0.2094  -2.322
## Phasesecond_postpool:indic_cherryOthers  -0.4105     0.1605  -2.557
## 
## Correlation of Fixed Effects:
##             (Intr) Phaspl Phssc_ indc_O Phs:_O
## Phasepool   -0.692                            
## Phsscnd_pst -0.915  0.633                     
## indc_chrryO -0.670  0.464  0.613              
## Phspl:ndc_O  0.475 -0.716 -0.434 -0.708       
## Phsscnd_:_O  0.619 -0.428 -0.663 -0.924  0.654
################### BELOW TEMP: A SUPR
############### Percent of Increase in fitness
# Calcul_increase_fitness <- expand.grid(Phase = unique(data_sum$Phase),
#                                      Fruit_s = unique(data_sum$Fruit_s))
# 
# Calcul_increase_fitness$fitness_predicted <- predict(mod_Best, 
#                                                      newdata = Calcul_increase_fitness, 
#                                                      re.form= NA, type = "response")
# 
# ((Calcul_increase_fitness$fitness_predicted[Calcul_increase_fitness$Phase=="second_postpool"]-
#     Calcul_increase_fitness$fitness_predicted[Calcul_increase_fitness$Phase=="first_prepool"])/
#   abs(Calcul_increase_fitness$fitness_predicted[Calcul_increase_fitness$Phase=="first_prepool"]))*100
# 
# Calcul_increase_fitness

3.3 Plot

#Position dodge
pd <- ggplot2::position_dodge(0.3) # move them .05 to the left and right

#Extract slope and intercept
dat_predict_allfruits <- expand.grid(Generation =as.numeric(levels(as.factor(data_sum$Generation))),
                                     Fruit_s=unique(data_sum$Fruit_s))

dat_predict_allfruits$Phase <- ifelse(dat_predict_allfruits$Generation<= 7, "first_prepool",
                                     ifelse(dat_predict_allfruits$Generation>=12, "second_postpool", "pool"))
dat_predict_allfruits$indic_cherry<- ifelse(dat_predict_allfruits$Fruit_s=="Cherry", "Cherry", "Others")
dat_predict_allfruits$fitness_predicted <- predict(mod_Best, newdata = dat_predict_allfruits, 
                          re.form= NA, type = "response")

#REAL DATA
#Add G1 G6 and G7
TEMP_lineG1 <- c(rep(NA, 2), 1,rep(NA, 5))
TEMP_lineG6 <- c(rep(NA, 2), 6,rep(NA, 5))
TEMP_lineG7 <- c(rep(NA, 2), 7,rep(NA, 5))

TEMP_total <- rbind(data_sum,TEMP_lineG1,TEMP_lineG1,TEMP_lineG1,TEMP_lineG6,TEMP_lineG6,TEMP_lineG6,TEMP_lineG7,TEMP_lineG7,TEMP_lineG7)
TEMP_total$Fruit_s[TEMP_total$Generation == "6"] <- c("Strawberry", "Cranberry", "Cherry")
TEMP_total$Fruit_s[TEMP_total$Generation == "1"] <- c("Strawberry", "Cranberry", "Cherry")
TEMP_total$Fruit_s[TEMP_total$Generation == "7"] <- c("Strawberry", "Cranberry", "Cherry")
tail(TEMP_total)
##     Line    Fruit_s Generation Phase  N Nb_adults sd fitness se_fitness
## 251 <NA> Strawberry          6  <NA> NA        NA NA      NA         NA
## 252 <NA>  Cranberry          6  <NA> NA        NA NA      NA         NA
## 253 <NA>     Cherry          6  <NA> NA        NA NA      NA         NA
## 254 <NA> Strawberry          7  <NA> NA        NA NA      NA         NA
## 255 <NA>  Cranberry          7  <NA> NA        NA NA      NA         NA
## 256 <NA>     Cherry          7  <NA> NA        NA NA      NA         NA
##     indic_cherry indic_cranberry indic_strawberry
## 251         <NA>               6             <NA>
## 252         <NA>               6             <NA>
## 253         <NA>               6             <NA>
## 254         <NA>               7             <NA>
## 255         <NA>               7             <NA>
## 256         <NA>               7             <NA>
## Add label 
TEMP_anno <- data.frame(x1 = c(3.5, 3.5, 3.5, 10, 3.5, 10), 
                        x2 = c(9, 17.5, 17.5, 17.5,  17.5, 17.5),
                        y1 = c(1, 2, 2, 1.2, 2, 1.2), 
                        y2 = c(1.25, 2.25, 2.25, 1.45,  2.25, 1.45),
                        xstar = c(6.5, 10, 10, 14,  10, 14), 
                        ystar = c(1.5, 2.5, 2.5, 1.7, 2.5, 1.7),
                        lab = c("**", "***", "***", "**",  "***", "**"),
                        Fruit_s = c("Cherry", "Cherry", 
                                    "Cranberry", "Cranberry",
                                    "Strawberry", "Strawberry"), 
                        Line = NA)
TEMP_anno
##     x1   x2  y1   y2 xstar ystar lab    Fruit_s Line
## 1  3.5  9.0 1.0 1.25   6.5   1.5  **     Cherry   NA
## 2  3.5 17.5 2.0 2.25  10.0   2.5 ***     Cherry   NA
## 3  3.5 17.5 2.0 2.25  10.0   2.5 ***  Cranberry   NA
## 4 10.0 17.5 1.2 1.45  14.0   1.7  **  Cranberry   NA
## 5  3.5 17.5 2.0 2.25  10.0   2.5 *** Strawberry   NA
## 6 10.0 17.5 1.2 1.45  14.0   1.7  ** Strawberry   NA
TEMP_title <- data.frame(xtitle = c(14, 14, 14),
                       ytitle = c(3, 3, 3),
                       title = c("Cherry", "Cranberry", "Strawberry"),
                       Fruit_s = c("Cherry", "Cranberry", "Strawberry"), 
                       Line = NA)
TEMP_title
##   xtitle ytitle      title    Fruit_s Line
## 1     14      3     Cherry     Cherry   NA
## 2     14      3  Cranberry  Cranberry   NA
## 3     14      3 Strawberry Strawberry   NA
PLOT_FITNESS_CHERRY <- ggplot(data = TEMP_total[TEMP_total$Fruit_s == "Cherry",], 
                            aes(x = factor(Generation),group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin =fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.2,color = "black") + 
  geom_line(size = 0.3,position = pd) + 
  geom_line(data = dat_predict_allfruits[dat_predict_allfruits$Fruit_s == "Cherry",], 
                     aes(x = factor(Generation), y = fitness_predicted,
                         colour = "black", group = Phase), size = 0.5) + 
  geom_point(size =1, position = pd, shape =21, fill = "white") + 
  ylim(-3, 3.05) + 
  ylab("Fitness") + 
  xlab("Generation") + 
  geom_text(data = TEMP_anno[TEMP_anno$Fruit_s == "Cherry",], aes(x = xstar,  y = ystar, label = lab), size =3.3) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cherry",], aes(x = x1, xend = x1, 
           y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cherry",], aes(x = x2, xend = x2, 
           y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cherry",], aes(x = x1, xend = x2, 
           y = y2, yend = y2), size = 0.4) + 
  scale_color_manual(values = c("black", "#BC3C6D", "#FDB424", "#3FAA96")) + 
  ggtitle("Cherry") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#BC3C6D"))
PLOT_FITNESS_CHERRY

PLOT_FITNESS_CRANB <- ggplot2::ggplot(data = TEMP_total[TEMP_total$Fruit_s == "Cranberry",], 
                            aes(x = factor(Generation),group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin =fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.2,color = "black") + 
  geom_line(size = 0.3,position = pd) + 
  geom_line(data = dat_predict_allfruits[dat_predict_allfruits$Fruit_s == "Cranberry",], aes(x = factor(Generation), y = fitness_predicted, colour = "black", group = Phase),
                size = 0.5) + 
  geom_point(size =1, position = pd, shape =21, fill = "white") + 
  ylim(-3, 3.05) + 
  ylab("Fitness") + 
  xlab("Generation") + 
  geom_text(data = TEMP_anno[TEMP_anno$Fruit_s == "Cranberry",], aes(x = xstar,  y = ystar, label = lab), size =3.3) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cranberry",], aes(x = x1, xend = x1, 
           y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cranberry",], aes(x = x2, xend = x2, 
           y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cranberry",], aes(x = x1, xend = x2, 
           y = y2, yend = y2), size = 0.4) + 
  scale_color_manual(values = c("black", "#FDB424")) + 
  ggtitle("Cranberry") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#FDB424"))
PLOT_FITNESS_CRANB

PLOT_FITNESS_STRAW <- ggplot2::ggplot(data = TEMP_total[TEMP_total$Fruit_s == "Strawberry",], 
                            aes(x = factor(Generation),group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin =fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.2,color = "black") + 
  geom_line(size = 0.3,position = pd) + 
  geom_line(data = dat_predict_allfruits[dat_predict_allfruits$Fruit_s == "Strawberry",], 
            aes(x = factor(Generation), y = fitness_predicted, colour = "black", group = Phase),
            size = 0.5) + 
  geom_point(size =1, position = pd, shape =21, fill = "white") + 
  ylim(-3, 3.05) + 
  ylab("Fitness") + 
  xlab("Generation") + 
  geom_text(data = TEMP_anno[TEMP_anno$Fruit_s == "Strawberry",], 
            aes(x = xstar,  y = ystar, label = lab), size =3.3) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Strawberry",], 
               aes(x = x1, xend = x1, y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Strawberry",], 
               aes(x = x2, xend = x2, y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Strawberry",], 
               aes(x = x1, xend = x2, y = y2, yend = y2), size = 0.4) + 
  scale_color_manual(values = c("black", "#3FAA96")) + 
  ggtitle("Strawberry") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#3FAA96"))
PLOT_FITNESS_STRAW

DYNAMIQUE_THREE_JOIN <- cowplot::ggdraw() + 
  cowplot::draw_plot(PLOT_FITNESS_CHERRY + theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank()), 
            x = 0, y = 0.66, width = 1, height = 0.33) + 
  cowplot::draw_plot(PLOT_FITNESS_CRANB + theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank()), 
            x = 0, y = 0.33, width = 1, height = 0.33) + 
  cowplot::draw_plot(PLOT_FITNESS_STRAW, 
            x = 0, y = 00, width = 1, height = 0.33) + 
  cowplot::draw_plot_label(c("A", "B", "C"),  
                  x = c(0, 0, 0), 
                  y = c(1, 0.66, 0.33), 
                  hjust = c(-0.5, -0.5, -0.5), 
                  vjust = c(1.5, 1.5, 1.5),
                  size = 12) 
 DYNAMIQUE_THREE_JOIN

cowplot::save_plot(file =here::here("figures", "FIG_Adaptation.pdf"), DYNAMIQUE_THREE_JOIN, base_height = 17/cm(1), base_width = 11/cm(1), dpi = 1200)

4 HETEROGENEITY

4.1 Plot

pd <-  position_dodge(width = 0.5)



################### INTERMEDIATE PHENOTYPING

# Re-order levels of Line
data_sum_G7 <- data_logchange[data_logchange$Generation == "7",]
data_sum_G7$Line <- factor(data_sum_G7$Line, levels= c("CE2", "CE1", "CE4", "CE3",
                                                       "CR2", "CR3", "CR5", "CR1", "CR4",
                                                       "FR2", "FR3", "FR5", "FR1", "FR4"))


#Plot: 
symp_allop_g7 <- ggplot(data = data_sum_G7, 
                      aes(x = Line, y = logchange, group = Treatment, 
                          color = Fruit_s, shape = Treatment, fill = Fruit_s)) + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey", size = 0.5) + 
  geom_errorbar(aes(ymin = logchange-1.96*sd_logchange, 
                    ymax =logchange + 1.96*sd_logchange, linetype = Line_type),
                  width = 0.2, size = 0.5, alpha = 0.6,position = pd) + 
  geom_point(position = pd, fill = "white", size =3) + 
  ylab("Fitness difference between intermediate\nand initial phenotyping steps")  + 
  xlab("Populations") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  guides(color = FALSE,
           shape = guide_legend(override.aes = list(fill = c("black")))) + 
  scale_shape_manual(values = c(21, 22, 24)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_fill_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  theme_LO_sober + theme(axis.text.x  = element_blank()) +
  coord_cartesian(expand = FALSE, ylim = c(-2, 2), xlim=c(0, 15),clip = "off") + 
  ggtitle("Intermediate phenotyping step") + 
  annotate("text", x = 2.5, y =1.5, label = 'bold(" Evolved\non cherry")',
                     size =3,colour = "#BC3C6D",parse = TRUE) + 
  annotate("text", x = 7.5, y =1.5, label = 'bold("   Evolved\non cranberry")',
                     size =3,colour = "#FDB424",parse = TRUE) + 
  annotate("text", x = 12.5, y =1.5, label = 'bold("   Evolved\non strawberry")',
                     size =3,colour = "#3FAA96",parse = TRUE) + 
  geom_segment(x = 15.5, y = -0.1, xend= 15.5, yend = -1.2, size = 0.15, 
               arrow = arrow(length = unit(0.05, "npc")),colour = "black") + 
  geom_segment(x = 15.5, y = 0.1, xend= 15.5, yend = 1.2, size = 0.15,
               arrow = arrow(length = unit(0.05, "npc")),colour = "black") + 
  annotate("text", x = 16.2, y = 0.5, label = 'bold(" Fitness\nincrease")',
             size =3,colour = "black",parse = TRUE, angle =90) + 
  annotate("text", x = 16.2, y =-0.5, label = 'bold("  Fitness\ndecrease")',
              size =3,colour = "black",parse = TRUE, angle =90) 
symp_allop_g7

################### FINAL PHENOTYPING

# Re-order levels of Line
data_sum_G29 <- data_logchange[data_logchange$Generation == "29",]
data_sum_G29$Line <- factor(data_sum_G29$Line, levels = c("CEA", "CEB", "CEC", 
                                                          "CRD", "CRA", "CRC", "CRB", "CRE",
                                                          "FRA", "FRC", "FRB"))

symp_allop_G29 <- ggplot(data = data_sum_G29, 
              aes(x = Line, y=logchange, group = Treatment, 
                  color =Fruit_s, shape = Treatment,fill =Fruit_s)) + 
      geom_hline(yintercept = 0, linetype = "dashed", color = "grey", size = 0.5) + 
    geom_point(size =3, position = pd) + 
    geom_errorbar(aes(ymin =logchange-1.96*sd_logchange, ymax =logchange + 1.96*sd_logchange),
                width= 0.2, size = 0.5, alpha = 0.6,position = pd) + 
    ylab("Fitness difference between final\nand initial phenotyping steps")  + 
    xlab("Populations") + 
    labs(shape = "Test fruit", color = "Selection fruit") + 
    guides(fill = FALSE, alpha = FALSE) + 
    scale_shape_manual(values = c(21, 22, 24)) + 
    scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    scale_fill_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    theme_LO_sober + theme(axis.text.x  = element_blank()) +
    coord_cartesian(expand = FALSE, ylim = c(-2, 2), xlim=c(0, 11),clip = "off") + 
  annotate("text", x = 2, y =1.5, label = 'bold(" Evolved\non cherry")',
                     size =3,colour = "#BC3C6D",parse = TRUE) + 
  annotate("text", x = 6, y =1.5, label = 'bold("   Evolved\non cranberry")',
                     size =3,colour = "#FDB424",parse = TRUE) + 
  annotate("text", x = 10, y =1.5, label = 'bold("   Evolved\non strawberry")',
                     size =3,colour = "#3FAA96",parse = TRUE) + 
  geom_segment(x = 11.5, y = -0.1, xend= 11.5, yend = -1.2, size = 0.15, 
               arrow = arrow(length = unit(0.05, "npc")),colour = "black") + 
    geom_segment(x = 11.5, y = 0.1, xend= 11.5, yend = 1.2, size = 0.15,
               arrow = arrow(length = unit(0.05, "npc")),colour = "black") + 
    annotate("text", x = 12, y = 0.5, label = 'bold(" Fitness\nincrease")',
             size =3,colour = "black",parse = TRUE, angle =90) + 
    annotate("text", x = 12, y = -0.5, label = 'bold("  Fitness\ndecrease")',
              size =3,colour = "black", parse = TRUE, angle =90) + 
  ggtitle("Final phenotyping step") 
symp_allop_G29

### Add stroke
DIF_FITNESS_G7 <- symp_allop_g7 + 
  geom_point(aes(alpha = SA, color = interaction(SA,Fruit_s)), position = pd, size =3, stroke =1.5, fill = "white") + 
  scale_alpha_manual(values = c(0, 1)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96", "#7C2748", "#CA8702", "#328677", "#BC3C6D", "#FDB424", "#3FAA96"))  
DIF_FITNESS_G7

DIF_FITNESS_G29 <- symp_allop_G29 + geom_point(aes(alpha = SA, color =interaction(SA,Fruit_s)), position = pd, size =3, stroke =1.5) + 
      scale_alpha_manual(values = c(0, 1)) + 
      scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96", "#7C2748", "#CA8702", "#328677", "#BC3C6D", "#FDB424", "#3FAA96"))  
DIF_FITNESS_G29

plot_legend <- ggplot(data = data_logchange, aes(x = Line, y=logchange, group = Treatment, 
                  color =Fruit_s, shape = Treatment,fill =Fruit_s)) + 
    geom_point(size =3, position = pd) + 
    labs(shape = "Test fruit", color = "Selection fruit") + 
    scale_shape_manual(values = c(21, 22, 24)) + 
    scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    theme_LO_sober + 
    guides(fill = FALSE) + 
    theme(legend.key.size = unit(0.5, "cm"),
        axis.text.x  = element_blank()) +
    coord_cartesian(expand = FALSE, ylim = c(-2, 2), xlim=c(0, 11),clip = "off") 


###########################################################
#############################################ALL PLOT
legend_trade <- lemon::g_legend(plot_legend)
    

SYMP_ALLOP_TOTAL <- cowplot::ggdraw() + 
  cowplot::draw_plot(DIF_FITNESS_G7 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm")),
            x = 0, y = 0.5, width = 1, height = 0.5) + 
  cowplot::draw_plot(DIF_FITNESS_G29 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm")), 
            x = 0, y = 0, width = 1, height = 0.5) + 
  cowplot::draw_plot_label(c("A", "B"),  
                  x = c(0, 0), y = c(1, 0.5), 
                  size = 14) + 
  cowplot::draw_plot(legend_trade, x = 0.93, y = 0.5, width = 0.001, height = 0.001) 

SYMP_ALLOP_TOTAL

cowplot::save_plot(file =here::here("figures", "FIG_Heterogeneity.pdf"),  
                   SYMP_ALLOP_TOTAL, base_height = 20/cm(1), base_width = 20/cm(1), dpi = 1200)

5 CORRELATED RESPONSES

5.1 Analysis MA and correlation

# ##### Create empty dataframe with slopes and confidence interval for all pairwise
Estimates_pairwise <- data.frame(Variables = rep(rep(c("correlation","cor_CI_inf","cor_CI_sup",
                                                       "slope", "intercept"),2),3),
                                 Generation = rep(c(rep(7, 5), rep(29, 5)),3),
                                 Pairwise = rep(c("Cherry_Cranberry",
                                                     "Cranberry_Strawberry", 
                                                     "Strawberry_Cherry"), each=10),
                                Estimates = NA )

## Nbr sample 
nb_sim = 500


#########################
######################### G7
#########################

####### 
#######  Cherry Cranberry
####### 


# MA axis regression with bootstrap to estimate slope, intercept and 
sim <- data.frame(t(sapply(1:nb_sim, bootstrap_lmodel2, data_pairwise = TEMP_dataG7_CheCran)))

# Fill out data with quantile of simulations
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="correlation"] <- mean(sim$correlation.cor, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="cor_CI_inf"] <- quantile(sim$correlation.cor, probs = 0.025, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="cor_CI_sup"] <- quantile(sim$correlation.cor, probs = 0.975, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="intercept"] <- mean(sim$intercept, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="slope"] <- mean(sim$slope, na.rm = TRUE)





####### 
#######  Cranberry Strawberry
####### 

# MA axis regression with bootstrap to estimate slope, intercept and 
sim <- data.frame(t(sapply(1:nb_sim, bootstrap_lmodel2, data_pairwise = TEMP_dataG7_CranStraw)))

# Fill out data with quantile of simulations
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="correlation"] <- quantile(sim$cor, probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="cor_CI_inf"] <- quantile(sim$cor, probs = 0.025, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="cor_CI_sup"] <- quantile(sim$cor, probs = 0.975, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="intercept"] <- quantile(sim$intercept,probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="slope"] <- quantile(sim$slope,probs = 0.5, na.rm = TRUE)






####### 
#######  Strawberry  Cherry
####### 



# MA axis regression with bootstrap to estimate slope, intercept and 
sim <- data.frame(t(sapply(1:nb_sim, bootstrap_lmodel2, data_pairwise = TEMP_dataG7_StrawChe)))

# Fill out data with quantile of simulations
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="correlation"] <- quantile(sim$cor, probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="cor_CI_inf"] <- quantile(sim$cor, probs = 0.025, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="cor_CI_sup"] <- quantile(sim$cor, probs = 0.975, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="intercept"] <- quantile(sim$intercept,probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==7&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="slope"] <- quantile(sim$slope,probs = 0.5, na.rm = TRUE)






#########################
######################### G29
######################### 


####### 
#######  Cherry Cranberry
####### 

# MA axis regression with bootstrap to estimate slope, intercept and 
sim <- data.frame(t(sapply(1:nb_sim, bootstrap_lmodel2, data_pairwise = TEMP_dataG29_CheCran)))

# Fill out data with quantile of simulations
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="correlation"] <- quantile(sim$cor, probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="cor_CI_inf"] <- quantile(sim$cor, probs = 0.025, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="cor_CI_sup"] <- quantile(sim$cor, probs = 0.975, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="intercept"] <- quantile(sim$intercept, probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cherry_Cranberry"&
                              Estimates_pairwise$Variables=="slope"] <- quantile(sim$slope, probs = 0.5, na.rm = TRUE)




####### 
#######  Cranberry Strawberry
####### 

# MA axis regression with bootstrap to estimate slope, intercept and 
sim <- data.frame(t(sapply(1:nb_sim, bootstrap_lmodel2, data_pairwise = TEMP_dataG29_CranStraw)))

# Fill out data with quantile of simulations
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="correlation"] <- quantile(sim$cor, probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="cor_CI_inf"] <- quantile(sim$cor, probs = 0.025, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="cor_CI_sup"] <- quantile(sim$cor, probs = 0.975, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="intercept"] <- quantile(sim$intercept,probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Cranberry_Strawberry"&
                              Estimates_pairwise$Variables=="slope"] <- quantile(sim$slope,probs = 0.5, na.rm = TRUE)



####### 
#######  Strawberry  Cherry
####### 
rm(sim)
# MA axis regression with bootstrap to estimate slope, intercept and 
sim <- data.frame(t(sapply(1:nb_sim, bootstrap_lmodel2, data_pairwise = TEMP_dataG29_StrawChe)))

# Fill out data with quantile of simulations
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="correlation"] <- quantile(sim$cor, probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="cor_CI_inf"] <- quantile(sim$cor, probs = 0.025, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="cor_CI_sup"] <- quantile(sim$cor, probs = 0.975, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="intercept"] <- quantile(sim$intercept,probs = 0.5, na.rm = TRUE)
Estimates_pairwise$Estimates[Estimates_pairwise$Generation==29&
                              Estimates_pairwise$Pairwise=="Strawberry_Cherry"&
                              Estimates_pairwise$Variables=="slope"] <- quantile(sim$slope,probs = 0.5, na.rm = TRUE)



Estimates_pairwise
##      Variables Generation             Pairwise   Estimates
## 1  correlation          7     Cherry_Cranberry  0.51235824
## 2   cor_CI_inf          7     Cherry_Cranberry -0.26985112
## 3   cor_CI_sup          7     Cherry_Cranberry  0.98513182
## 4        slope          7     Cherry_Cranberry  4.78676276
## 5    intercept          7     Cherry_Cranberry -0.89344304
## 6  correlation         29     Cherry_Cranberry -0.66081010
## 7   cor_CI_inf         29     Cherry_Cranberry -0.98341734
## 8   cor_CI_sup         29     Cherry_Cranberry  0.03114952
## 9        slope         29     Cherry_Cranberry -1.58942013
## 10   intercept         29     Cherry_Cranberry  0.23790858
## 11 correlation          7 Cranberry_Strawberry  0.66688074
## 12  cor_CI_inf          7 Cranberry_Strawberry  0.21569369
## 13  cor_CI_sup          7 Cranberry_Strawberry  0.92784591
## 14       slope          7 Cranberry_Strawberry  1.24112298
## 15   intercept          7 Cranberry_Strawberry -0.04634633
## 16 correlation         29 Cranberry_Strawberry  0.60888143
## 17  cor_CI_inf         29 Cranberry_Strawberry  0.09598369
## 18  cor_CI_sup         29 Cranberry_Strawberry  0.93016894
## 19       slope         29 Cranberry_Strawberry  1.44473566
## 20   intercept         29 Cranberry_Strawberry -0.48298442
## 21 correlation          7    Strawberry_Cherry  0.66109984
## 22  cor_CI_inf          7    Strawberry_Cherry  0.05864868
## 23  cor_CI_sup          7    Strawberry_Cherry  0.99009143
## 24       slope          7    Strawberry_Cherry  1.16489520
## 25   intercept          7    Strawberry_Cherry  0.05721628
## 26 correlation         29    Strawberry_Cherry  0.72302682
## 27  cor_CI_inf         29    Strawberry_Cherry  0.38397534
## 28  cor_CI_sup         29    Strawberry_Cherry  0.99729944
## 29       slope         29    Strawberry_Cherry  2.51811211
## 30   intercept         29    Strawberry_Cherry -0.79553355

5.2 Plot

ymin = -50
ymax = 50


##################################################
##################   G7
# Limits
ymin_CheCranG7=min(min(TEMP_dataG7_CheCran$logchange_allop-1.96*TEMP_dataG7_CheCran$sd_allop, na.rm= TRUE),
                min(TEMP_dataG7_CheCran$logchange_symp-1.96*TEMP_dataG7_CheCran$sd_symp, na.rm= TRUE))
ymax_CheCranG7=max(max(TEMP_dataG7_CheCran$logchange_allop + 1.96*TEMP_dataG7_CheCran$sd_allop, na.rm= TRUE),
                max(TEMP_dataG7_CheCran$logchange_symp + 1.96*TEMP_dataG7_CheCran$sd_symp, na.rm= TRUE))


## Function equation R
eq_r <- function(gen = 7, pair = "Cherry_Cranberry"){
eq <- substitute(~~italic(r)^2~"="~r2,
list(r2 = format(Estimates_pairwise$Estimates[Estimates_pairwise$Generation == gen &
Estimates_pairwise$Pairwise == pair &
Estimates_pairwise$Variables == "correlation"], digits = 2)))
as.character(as.expression(eq));
}



# Plot
CheCran_G7 <- ggplot(data = TEMP_dataG7_CheCran) + 
  geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Cherry_Cranberry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Cherry_Cranberry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_text(x = 0.7, y = -1.2, label = eq_r(gen = 7, pair = "Cherry_Cranberry"), parse = TRUE, color="black") +
  geom_errorbar(aes(x = logchange_symp, ymin = logchange_allop-(1.96*sd_allop),
                    ymax = logchange_allop + (1.96*sd_allop),
                    color = Symp, linetype = Line_type),
                width= 0.02, size = 0.3, alpha = 0.8) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = logchange_symp-1.96*sd_symp, xmax = logchange_symp + 1.96*sd_symp, 
                     color = Symp, linetype = Line_type),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
  geom_point(aes(x = logchange_symp, y = logchange_allop,  color = Symp,fill = Symp, shape = Allop),
             size = 3, fill = "white", stroke =1.2) + 
  xlab("Fitness difference in\nselective environment")  + 
  ylab("Fitness difference in\nalternative environment")  + 
  ggtitle("Cherry vs. Cranberry") + 
  theme_LO_sober + 
  labs(shape = "Test fruit", color = "Evolution fruit") + 
  scale_shape_manual(values = c(21, 22)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424"))  + 
  coord_cartesian(ylim = c(ymin_CheCranG7, ymax_CheCranG7), 
                  xlim = c(ymin_CheCranG7, ymax_CheCranG7)) 
  
 CheCran_G7

# Limits
ymin_CranStrawG7=min(min(TEMP_dataG7_CranStraw$logchange_allop-1.96*TEMP_dataG7_CranStraw$sd_allop, na.rm= TRUE),
                min(TEMP_dataG7_CranStraw$logchange_symp-1.96*TEMP_dataG7_CranStraw$sd_symp, na.rm= TRUE))
ymax_CranStrawG7=max(max(TEMP_dataG7_CranStraw$logchange_allop + 1.96*TEMP_dataG7_CranStraw$sd_allop, na.rm= TRUE),
                max(TEMP_dataG7_CranStraw$logchange_symp + 1.96*TEMP_dataG7_CranStraw$sd_symp, na.rm= TRUE))

CranStraw_G7 <-  ggplot(data = TEMP_dataG7_CranStraw) + 
  geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Cranberry_Strawberry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Cranberry_Strawberry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_errorbar(aes(x =logchange_symp, ymin = logchange_allop-(1.96*sd_allop), 
                    ymax = logchange_allop + (1.96*sd_allop),
                    color = Symp, linetype = Line_type),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = logchange_symp-1.96*sd_symp, xmax = logchange_symp + 1.96*sd_symp, 
                 color = Symp, linetype = Line_type),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
  geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Symp,fill = Symp, shape = Allop),
                   size =3, fill = "white", stroke =1.2) + 
  geom_text(x = -0.25, y = -0.95, label = eq_r(gen = 7, pair = "Cranberry_Strawberry"), parse = TRUE, color="black") +
  xlab("Fitness difference in\nselective environment")  + 
  ylab("Fitness difference in\nalternative environment")  + 
     ggtitle("Cranberry vs. Strawberry") + 
  coord_cartesian(ylim = c(ymin_CranStrawG7, ymax_CranStrawG7), 
                  xlim = c(ymin_CranStrawG7, ymax_CranStrawG7)) + 
   labs(shape = "Test fruit", color = "Selection fruit") + 
   scale_shape_manual(values = c(22, 24)) + 
   scale_color_manual(values = c("#FDB424", "#3FAA96"))  + 
  theme_LO_sober
 CranStraw_G7

# Limits
ymin_StrawCheG7=min(min(TEMP_dataG7_StrawChe$logchange_allop-1.96*TEMP_dataG7_StrawChe$sd_allop, na.rm= TRUE),
                min(TEMP_dataG7_StrawChe$logchange_symp-1.96*TEMP_dataG7_StrawChe$sd_symp, na.rm= TRUE))
ymax_StrawCheG7=max(max(TEMP_dataG7_StrawChe$logchange_allop + 1.96*TEMP_dataG7_StrawChe$sd_allop, na.rm= TRUE),
                max(TEMP_dataG7_StrawChe$logchange_symp + 1.96*TEMP_dataG7_StrawChe$sd_symp, na.rm= TRUE))

StrawChe_G7 <-  ggplot(data = TEMP_dataG7_StrawChe) + 
    geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Strawberry_Cherry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Strawberry_Cherry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_errorbar(aes(x =logchange_symp, ymin = logchange_allop-(1.96*sd_allop), ymax = logchange_allop + (1.96*sd_allop),
                    color = Symp, linetype = Line_type),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = logchange_symp-1.96*sd_symp, xmax = logchange_symp + 1.96*sd_symp, 
                 color = Symp, linetype = Line_type),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
  geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Symp,fill = Symp, shape = Allop),
                 size =3, fill = "white", stroke =1.2) + 
  geom_text(x = 0.5, y = -1.3, label = eq_r(gen = 7, pair = "Strawberry_Cherry"), parse = TRUE, color="black") +
  xlab("Fitness difference in\nselective environment")  + 
  ylab("Fitness difference in\nalternative environment")  + 
  ggtitle("Strawberry vs. Cherry") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  scale_shape_manual(values = c(21, 24)) + 
  scale_color_manual(values = c("#BC3C6D", "#3FAA96"))  + 
  coord_cartesian(ylim = c(ymin_StrawCheG7, ymax_StrawCheG7), 
                  xlim = c(ymin_StrawCheG7, ymax_StrawCheG7)) + 
  theme_LO_sober
 StrawChe_G7

#####################################################################################
##################################      G29        ##################################
#####################################################################################
TEMP_dataG29_CheCran
##    Line Treatment   Fruit_s Generation   logchange sd_logchange SA      Symp
## 14  CEA Cranberry    Cherry         29 -0.21774071   0.13281291  0    Cherry
## 17  CEB Cranberry    Cherry         29 -0.59200204   0.09634940  0    Cherry
## 21  CEC Cranberry    Cherry         29 -0.20670829   0.12591289  0    Cherry
## 37  CRA    Cherry Cranberry         29  0.32537794   0.10025669  0 Cranberry
## 42  CRB    Cherry Cranberry         29  0.16929862   0.12699030  0 Cranberry
## 45  CRC    Cherry Cranberry         29  0.08474123   0.12334598  0 Cranberry
## 47  CRD    Cherry Cranberry         29  0.54736475   0.09718018  0 Cranberry
## 49  CRE    Cherry Cranberry         29 -0.10496044   0.11556266  0 Cranberry
##        Allop Line_type logchange_allop   sd_allop logchange_symp   sd_symp
## 14 Cranberry    dashed     -0.21774071 0.13281291     0.14767000 0.1574284
## 17 Cranberry    dashed     -0.59200204 0.09634940     0.25038029 0.1009864
## 21 Cranberry    dashed     -0.20670829 0.12591289     0.35854062 0.1169300
## 37    Cherry    dashed      0.32537794 0.10025669    -0.14885350 0.1193552
## 42    Cherry    dashed      0.16929862 0.12699030     0.29719689 0.1531197
## 45    Cherry    dashed      0.08474123 0.12334598     0.02703717 0.1156381
## 47    Cherry    dashed      0.54736475 0.09718018    -0.15627744 0.1156695
## 49    Cherry    dashed     -0.10496044 0.11556266     0.72513486 0.1275430
TEMP_dataG29_CranStraw
##    Line  Treatment    Fruit_s Generation   logchange sd_logchange SA       Symp
## 39  CRA Strawberry  Cranberry         29 -0.29731422    0.1098141  0  Cranberry
## 40  CRB Strawberry  Cranberry         29 -0.24539416    0.1387043  0  Cranberry
## 43  CRC Strawberry  Cranberry         29 -0.28950778    0.2012393  0  Cranberry
## 48  CRD Strawberry  Cranberry         29 -1.02555272    0.1902082  0  Cranberry
## 51  CRE Strawberry  Cranberry         29 -0.16810249    0.1398057  0  Cranberry
## 69  FRA  Cranberry Strawberry         29 -0.25158762    0.1712755  0 Strawberry
## 71  FRB  Cranberry Strawberry         29  0.04303751    0.1441119  0 Strawberry
## 74  FRC  Cranberry Strawberry         29  0.52109773    0.1254820  0 Strawberry
##         Allop Line_type logchange_allop  sd_allop logchange_symp   sd_symp
## 39 Strawberry    dashed     -0.29731422 0.1098141    -0.14885350 0.1193552
## 40 Strawberry    dashed     -0.24539416 0.1387043     0.29719689 0.1531197
## 43 Strawberry    dashed     -0.28950778 0.2012393     0.02703717 0.1156381
## 48 Strawberry    dashed     -1.02555272 0.1902082    -0.15627744 0.1156695
## 51 Strawberry    dashed     -0.16810249 0.1398057     0.72513486 0.1275430
## 69  Cranberry    dashed     -0.25158762 0.1712755     0.15709032 0.1411560
## 71  Cranberry    dashed      0.04303751 0.1441119     0.56023100 0.1284283
## 74  Cranberry    dashed      0.52109773 0.1254820     0.36640738 0.1201170
TEMP_dataG29_StrawChe
##    Line  Treatment    Fruit_s Generation   logchange sd_logchange SA       Symp
## 13  CEA Strawberry     Cherry         29  0.01971359    0.1475176  0     Cherry
## 16  CEB Strawberry     Cherry         29 -0.15850680    0.0983504  0     Cherry
## 19  CEC Strawberry     Cherry         29  0.21131464    0.1237821  0     Cherry
## 68  FRA     Cherry Strawberry         29 -0.50599095    0.1378652  0 Strawberry
## 72  FRB     Cherry Strawberry         29  0.19377147    0.1094819  0 Strawberry
## 75  FRC     Cherry Strawberry         29  0.32151693    0.1140574  0 Strawberry
##         Allop Line_type logchange_allop  sd_allop logchange_symp   sd_symp
## 13 Strawberry    dashed      0.01971359 0.1475176      0.1476700 0.1574284
## 16 Strawberry    dashed     -0.15850680 0.0983504      0.2503803 0.1009864
## 19 Strawberry    dashed      0.21131464 0.1237821      0.3585406 0.1169300
## 68     Cherry    dashed     -0.50599095 0.1378652      0.1570903 0.1411560
## 72     Cherry    dashed      0.19377147 0.1094819      0.5602310 0.1284283
## 75     Cherry    dashed      0.32151693 0.1140574      0.3664074 0.1201170
# Limits
ymin_CheCranG29=min(min(TEMP_dataG29_CheCran$logchange_allop-1.96*TEMP_dataG29_CheCran$sd_allop, na.rm= TRUE),
                min(TEMP_dataG29_CheCran$logchange_symp-1.96*TEMP_dataG29_CheCran$sd_symp, na.rm= TRUE))
ymax_CheCranG29=max(max(TEMP_dataG29_CheCran$logchange_allop + 1.96*TEMP_dataG29_CheCran$sd_allop, na.rm= TRUE),
                max(TEMP_dataG29_CheCran$logchange_symp + 1.96*TEMP_dataG29_CheCran$sd_symp, na.rm= TRUE))


CheCran_G29 <- ggplot(data = TEMP_dataG29_CheCran) + 
    geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Cherry_Cranberry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Cherry_Cranberry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_errorbar(aes(x =logchange_symp, ymin = logchange_allop-(1.96*sd_allop), 
                    ymax = logchange_allop + (1.96*sd_allop),
                    color = Symp),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = logchange_symp-1.96*sd_symp, xmax = logchange_symp + 1.96*sd_symp, 
                 color = Symp),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
  geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Symp,fill = Symp, shape = Allop),
                 size =3, fill = "white", stroke =1.2) + 
  geom_text(x = -0.5, y = -0.4, label = eq_r(gen = 29, pair = "Cherry_Cranberry"), parse = TRUE, color="black") +
  xlab("Fitness difference in\nselective environment")  + 
  ylab("Fitness difference in\nalternative environment")  + 
  ggtitle("Cherry vs. Cranberry") + 
  labs(shape = "Test fruit", color = "Evolution fruit") + 
  scale_shape_manual(values = c(16, 15)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424"))  + 
  coord_cartesian(ylim = c(ymin_CheCranG29, ymax_CheCranG29), 
                  xlim = c(ymin_CheCranG29, ymax_CheCranG29)) + 
  theme_LO_sober
 CheCran_G29

# Limits
ymin_CranStrawG29=min(min(TEMP_dataG29_CranStraw$logchange_allop-1.96*TEMP_dataG29_CranStraw$sd_allop, na.rm= TRUE),
                min(TEMP_dataG29_CranStraw$logchange_symp-1.96*TEMP_dataG29_CranStraw$sd_symp, na.rm= TRUE))
ymax_CranStrawG29=max(max(TEMP_dataG29_CranStraw$logchange_allop + 1.96*TEMP_dataG29_CranStraw$sd_allop, na.rm= TRUE),
                max(TEMP_dataG29_CranStraw$logchange_symp + 1.96*TEMP_dataG29_CranStraw$sd_symp, na.rm= TRUE))

#Plot
 CranStraw_G29 <- ggplot(data = TEMP_dataG29_CranStraw) + 
  geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Cranberry_Strawberry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Cranberry_Strawberry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
   geom_errorbar(aes(x =logchange_symp, ymin = logchange_allop-(1.96*sd_allop), 
                    ymax = logchange_allop + (1.96*sd_allop),
                    color = Symp),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
   geom_errorbarh(aes(y = logchange_allop, xmin = logchange_symp-1.96*sd_symp, xmax = logchange_symp + 1.96*sd_symp, 
                 color = Symp),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
   geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Symp,fill = Symp, shape = Allop),
                 size =3, fill = "white", stroke =1.2) + 
   geom_text(x = -1, y = -0.8, label = eq_r(gen = 29, pair = "Cranberry_Strawberry"), parse = TRUE, color="black") +
   xlab("Fitness difference in\nselective environment")  + 
   ylab("Fitness difference in\nalternative environment")  + 
   ggtitle("Cranberry vs. Strawberry") + 
   coord_cartesian(ylim = c(ymin_CranStrawG29, ymax_CranStrawG29), 
                  xlim = c(ymin_CranStrawG29, ymax_CranStrawG29)) + 
   labs(shape = "Test fruit", color = "Selection fruit") + 
   scale_shape_manual(values = c(15, 17)) + 
   scale_color_manual(values = c("#FDB424", "#3FAA96"))  + 
   theme_LO_sober
 CranStraw_G29

# Limits
ymin_StrawCheG29=min(min(TEMP_dataG29_StrawChe$logchange_allop-1.96*TEMP_dataG29_StrawChe$sd_allop, na.rm= TRUE),
                min(TEMP_dataG29_StrawChe$logchange_symp-1.96*TEMP_dataG29_StrawChe$sd_symp, na.rm= TRUE))
ymax_StrawCheG29=max(max(TEMP_dataG29_StrawChe$logchange_allop + 1.96*TEMP_dataG29_StrawChe$sd_allop, na.rm= TRUE),
                max(TEMP_dataG29_StrawChe$logchange_symp + 1.96*TEMP_dataG29_StrawChe$sd_symp, na.rm= TRUE))


StrawChe_G29 <- ggplot(data = TEMP_dataG29_StrawChe) + 
 geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Strawberry_Cherry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Strawberry_Cherry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_errorbar(aes(x =logchange_symp, ymin = logchange_allop-(1.96*sd_allop), 
                    ymax = logchange_allop + (1.96*sd_allop),
                    color = Symp),
                  width= 0.02, size = 0.2, alpha =1) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = logchange_symp-1.96*sd_symp, xmax = logchange_symp + 1.96*sd_symp, 
                 color = Symp),
                 height = 0.02, size = 0.2, alpha =1) + 
  geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Symp,fill = Symp, shape = Allop),
                 size =3, fill = "white", stroke =1.2) + 
  geom_text(x = -0.4, y = -0.3, label = eq_r(gen = 29, pair = "Strawberry_Cherry"), parse = TRUE, color="black") +
  coord_cartesian(ylim = c(ymin_StrawCheG29, ymax_StrawCheG29), 
                  xlim = c(ymin_StrawCheG29, ymax_StrawCheG29)) + 
    xlab("Fitness difference in\nselective environment")  + 
    ylab("Fitness difference in\nalternative environment")  + 
  ggtitle("Strawberry vs. Cherry") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  scale_shape_manual(values = c(16, 17)) + 
  scale_color_manual(values = c("#BC3C6D", "#3FAA96"))  + 
  theme_LO_sober
 StrawChe_G29

legend_tradevide <-  ggplot(data = data_logchange[data_logchange$Generation == "29",],
                          aes(x =logchange, y = logchange,  color = Symp,fill = Symp, shape = Allop)) + 
  geom_point(size =2.5, fill = "white") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  scale_shape_manual(values = c(16, 15, 17)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  theme_LO_sober

legend_trade <- lemon::g_legend(legend_tradevide)

 
 

Slopeestimates_logchange_pairwise_errorbar <- cowplot::ggdraw() + 
  cowplot::draw_plot(CheCran_G7 + theme(legend.position = "none"), 
            x = 0.01, y = 0.5, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(CranStraw_G7 + theme(legend.position = "none"), 
            x = 0.31, y = 0.5, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(StrawChe_G7 + theme(legend.position = "none"), 
            x = 0.61, y = 0.5, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(legend_trade, x = 0.85, y = 0.5, width = 0.1, height = 0.1) + 
  cowplot::draw_plot(CheCran_G29 + theme(legend.position = "none"), 
            x = 0.01, y = 0, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(CranStraw_G29 + theme(legend.position = "none"), 
            x = 0.31, y = 0, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(StrawChe_G29 + theme(legend.position = "none"), 
            x = 0.61, y = 0, width = 0.22, height = 0.45) + 
  cowplot::draw_plot_label(c("Intermediate phenotyping step", "A", "B", "C", " ",
                    "Final phenotyping step", "D", "E", "F", " "),  
                  x = c(0.30, 0.01, 0.30, 0.61, 0.92, 0.30, 0.01, 0.30, 0.61, 0.92), 
                  y = c(1, 0.95, 0.95, 0.95, 0.95, 0.5, 0.45, 0.45, 0.45, 0.45), 
                  hjust = c(-0.25, -0.25, -0.25, -0.25, -0.25, -0.75, -0.75, -0.75, -0.75, -0.75), 
                  vjust = c(1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5),
                  size = 14) 
 Slopeestimates_logchange_pairwise_errorbar

#  
# #  
# 
cowplot::save_plot(file =here::here("figures", "FIG_Correlation.pdf"),
                   Slopeestimates_logchange_pairwise_errorbar,
                   base_height = 16/cm(1), base_width = 32/cm(1), dpi = 610)

6 SA AND LOCAL ADAPTATION

6.1 Analysis fitness difference

# Add vector of weight 
data_logchange$Inv_sum<-NA
data_logchange$Vector_sample<-NA
data_logchange[data_logchange$Generation=="7",]$Inv_sum <- 1 / (data_logchange[data_logchange$Generation=="7",]$sd_logchange^2)
data_logchange[data_logchange$Generation=="7",]$Vector_sample <- data_logchange[data_logchange$Generation=="7",]$Inv_sum /
  sum(data_logchange[data_logchange$Generation=="7",]$Inv_sum,na.rm = TRUE)

data_logchange[data_logchange$Generation=="29",]$Inv_sum <- 1 / (data_logchange[data_logchange$Generation=="29",]$sd_logchange^2)
data_logchange[data_logchange$Generation=="29",]$Vector_sample <- data_logchange[data_logchange$Generation=="29",]$Inv_sum /
  sum(data_logchange[data_logchange$Generation=="29",]$Inv_sum,na.rm = TRUE)

# Problem with NA (if one sd missing): replace by 0 (max value)
data_logchange$Vector_sample[is.na(data_logchange$Vector_sample)] <- 0


##### 
#####  G7
##### 
lm_val_G7 = lm(logchange ~ Treatment + Line + SA + Treatment:Fruit_s, 
               weights = Vector_sample, data = data_logchange[data_logchange$Generation=="7",])

Fratio = anova(lm_val_G7)[3, 3]/anova(lm_val_G7)[4, 3]
pvalue = 1 - pf(Fratio, anova(lm_val_G7)[3, 1], anova(lm_val_G7)[4, 1]) 

pvalue
## [1] 0.9777576
Fratio
## [1] 0.0009158865
anova(lm_val_G7)[3, 1]
## [1] 1
anova(lm_val_G7)[4, 1]
## [1] 3
##### 
#####  G29
##### 
lm_val_G29 = lm(logchange ~ Treatment + Line + SA + Treatment:Fruit_s, 
               weights = Vector_sample, data = data_logchange[data_logchange$Generation=="29",])

Fratio = anova(lm_val_G29)[3, 3]/anova(lm_val_G29)[4, 3]
pvalue = 1 - pf(Fratio, anova(lm_val_G29)[3, 1], anova(lm_val_G29)[4, 1]) 

pvalue
## [1] 0.211062
Fratio
## [1] 2.513403
anova(lm_val_G29)[3, 1]
## [1] 1
anova(lm_val_G29)[4, 1]
## [1] 3

6.2 TEMP: A tej Analysis fitness

##### 
#####  G7
##### 
# Pblm when Nb_adults= 0, it is impossible to calculate fitness log(0/20)
data_G7[data_G7$Nb_adults == 0,]
##      Treatment Line    Fruit_s Nb_eggs Nb_adults SA Emergence_rate
## 85  Strawberry  FR4 Strawberry      28         0  1              0
## 158  Cranberry  CE1     Cherry     365         0  0              0
## 159     Cherry  FR1 Strawberry     284         0  0              0
## 177     Cherry  CE1     Cherry     249         0  1              0
# correspond to 2 allopatric and 2 sympatric measurements (2 in CE1, 1 in FR4 et 1 in FR1)

# Remove Nb_adults = 0 because it is negligeable
data_G7_without0 <- data_G7[data_G7$Nb_adults!= 0,]
data_G7_without0$fitness <- log(data_G7_without0$Nb_adults/20)

lm_val_G7 = lm(fitness ~ Treatment + Line + SA + Treatment:Fruit_s, data = data_G7_without0)

Fratio = anova(lm_val_G7)[3, 3]/anova(lm_val_G7)[4, 3]
pvalue = 1 - pf(Fratio, anova(lm_val_G7)[3, 1], anova(lm_val_G7)[4, 1]) 

pvalue
## [1] 0.9570523
Fratio
## [1] 0.003418531
anova(lm_val_G7)[3, 1]
## [1] 1
anova(lm_val_G7)[4, 1]
## [1] 3
##### 
#####  G29
##### 

# Pblm when Nb_adults= 0, it is impossible to calculate fitness log(0/20)
data_G29[data_G29$Nb_adults == 0,]
##       Treatment Line    Fruit_s Nb_eggs Nb_adults SA Emergence_rate
## 5406     Cherry  CEA     Cherry      18         0  1              0
## 5494 Strawberry  CRA  Cranberry      56         0  0              0
## 5501 Strawberry  CRA  Cranberry      82         0  0              0
## 5535 Strawberry  CEB     Cherry      74         0  0              0
## 5555     Cherry  CRE  Cranberry      19         0  0              0
## 5563 Strawberry  CRE  Cranberry     113         0  0              0
## 5564 Strawberry  CRE  Cranberry     125         0  0              0
## 5629 Strawberry  CRB  Cranberry     106         0  0              0
## 5741     Cherry  CEA     Cherry      23         0  1              0
## 5769  Cranberry  FRB Strawberry      23         0  0              0
## 5831  Cranberry  CRA  Cranberry      40         0  1              0
## 5922 Strawberry  FRA Strawberry      82         0  1              0
## 5997 Strawberry  CRD  Cranberry      25         0  0              0
## 6059     Cherry  CEA     Cherry     187         0  1              0
## 6062 Strawberry  CEA     Cherry     195         0  0              0
## 6090 Strawberry  FRB Strawberry     143         0  1              0
## 6111     Cherry  FRB Strawberry     114         0  0              0
## 6240  Cranberry  FRA Strawberry     125         0  0              0
## 6293     Cherry  CEC     Cherry     188         0  1              0
## 6301     Cherry  CEC     Cherry     163         0  1              0
## 6309  Cranberry  CEC     Cherry     199         0  0              0
## 6324 Strawberry  CRD  Cranberry      82         0  0              0
## 6325 Strawberry  CRD  Cranberry      72         0  0              0
## 6327 Strawberry  CRD  Cranberry      58         0  0              0
## 6330 Strawberry  CRD  Cranberry      59         0  0              0
length(data_G29[data_G29$Nb_adults == 0 & data_G29$SA == 0,]$Nb_adults)
## [1] 17
length(data_G29[data_G29$Nb_adults == 0 & data_G29$SA == 1,]$Nb_adults)
## [1] 8
# correspond to 25 measures 17 allopatric and 8 sympatric measurements (2 in CE1, 1 in FR4 et 1 in FR1)

# Remove Nb_adults = 0 because it is negligeable
data_G29_without0 <- data_G29[data_G29$Nb_adults!= 0,]
data_G29_without0$fitness <- log(data_G29_without0$Nb_adults/20)

lm_val_G29 = lm(fitness ~ Treatment + Line + SA + Treatment:Fruit_s, data = data_G29_without0)

Fratio = anova(lm_val_G29)[3, 3]/anova(lm_val_G29)[4, 3]
pvalue = 1 - pf(Fratio, anova(lm_val_G29)[3, 1], anova(lm_val_G29)[4, 1]) 

pvalue
## [1] 0.1733915
Fratio
## [1] 3.1628
anova(lm_val_G29)[3, 1]
## [1] 1
anova(lm_val_G29)[4, 1]
## [1] 3